package aladyn;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.Set;
import aladyn.generator.LRSerializableGenerator;
import aladyn.xmlparser.LRSerializableXmlParser;

/**
 * 
 * @author YiHan WANG, Qi XU, Bo CAI
 *
 */
public class Main {
	
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		
		int argc = args.length;
		String fileName = "Fichier\\classes.xml";
		boolean verbose = false;
		
		
		if(argc < 1){
			printUsage();
			return;
		}else
			fileName = args[0];
		
		String arg;
		for(int i = 1; i < argc; i++){
			arg = args[i].toLowerCase();
			if("-verbose".equals(arg))
				verbose = true;
		}
		
		LRSerializableGenerator.setVerbose(verbose);
		LRSerializableXmlParser.setVerbose(verbose);
		
		try {
			ArrayList<String> classes = LRSerializableXmlParser.parse(new FileReader(fileName));
			for(String str : classes){
				LRSerializableGenerator.addLRSerializable(str);
			}
			
			LRSerializableGenerator.generateTypeMethodString();
			LRSerializableGenerator.generateTypeMethodStringStringTokenizer();
		} catch (Exception e) {
			throw e; 
		}
		
		if(verbose){
			Set<String> keys = LRSerializableGenerator.hashTab.keySet();
			System.out.println("Optimisation : int <-> class name");
			System.out.println("======================================================================");
			System.out.println("VALUE\tKEY");
			for(String key : keys){
				System.out.println(LRSerializableGenerator.hashTab.get(key) + "\t" + key);
			}
			System.out.println("======================================================================");
		}
	}
	
	public static void printUsage(){
		System.out.println("Usage : ");
		System.out.println("  serialisation_generator xml_file -verbose");
	}

}
